package com.yuaihen.databinding;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;
import com.yuaihen.databinding.adapter.EmployeeAdapter;
import com.yuaihen.databinding.bean.Employee;
import com.yuaihen.databinding.databinding.ActivityListBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yuaihen.
 * on 2018/12/10
 */
public class ListActivity extends AppCompatActivity {

    ActivityListBinding mBinding;
    private EmployeeAdapter employeeAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_list);
        mBinding.setPresenter(new Presenter());
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        employeeAdapter = new EmployeeAdapter(this);
        mBinding.recyclerView.setAdapter(employeeAdapter);

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(new Employee("zhou1", "hanjun1", false));
        employeeList.add(new Employee("zhou2", "hanjun2", false));
        employeeList.add(new Employee("zhou3", "hanjun3", true));
        employeeList.add(new Employee("zhou4", "hanjun4", false));
        employeeAdapter.addAll(employeeList);
        employeeAdapter.setListener(new EmployeeAdapter.OnItemClickListener() {
            @Override
            public void OnEmployeeClick(Employee employee) {
                Toast.makeText(ListActivity.this,
                        employee.getFirstName(), Toast.LENGTH_SHORT).show();

            }
        });

    }

    public class Presenter {

        public void onItemAddClick(View v) {
            employeeAdapter.add(new Employee("new ", "new1", false));
        }

        public void onItemRemoveClick(View v) {
            employeeAdapter.remove();
        }
    }
}
